package com.karina.kenny.gallary.chart.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.karina.visualization.chart.client.AreaChartWidget;
import com.karina.visualization.chart.client.BucketChartWidget;
import com.karina.visualization.chart.client.ColumnChartWidget;
import com.karina.visualization.chart.client.LineChartWidget;
import com.karina.visualization.chart.util.ChartModelUtil;

public class ChartGallary implements EntryPoint {

	@Override
	public void onModuleLoad() {
		TabPanel panel = new TabPanel();
		panel.setWidth("100%");
		panel.setHeight("100%");

		panel.add(createBucketChart(), "BucketChart");
		panel.add(createBarChart(), "ColumnChart");
		panel.add(createLineChart(), "LineChart");
		panel.add(createAreaChart(), "AreaChart");

		panel.selectTab(0);

		RootPanel.get().add(panel);
	}

	private Panel createLineChart() {
		VerticalPanel panel = new VerticalPanel();
		final LineChartWidget<Map<String, Object>> lineChart = new LineChartWidget<Map<String, Object>>();
		lineChart.setWidth("500");
		lineChart.setHeight("300");
		// lineChart.enableEffect(true);
		lineChart.setDataProvider(createdataList());
		lineChart.setXSeriesProperty("time");

		lineChart.setChartStyle("LineSeries", "lineStyle", "curve");

		Timer timer = new Timer() {

			@Override
			public void run() {
				lineChart.setDataProvider(createdataList());
			}
		};

		timer.scheduleRepeating(4000);

		final CheckBox checkbox = new CheckBox("Enable Animation");
		checkbox.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				lineChart.enableEffect(checkbox.getValue());
			}
		});
		panel.add(lineChart);
		panel.add(checkbox);
		return panel;
	}

	private Panel createBucketChart() {
		VerticalPanel panel = new VerticalPanel();
		final BucketChartWidget chart = new BucketChartWidget();
		chart.setWidth("500");
		chart.setHeight("300");
		chart.enableEffect(true);
		chart.setChartStyle("BucketChart.BucketSeries", "width", 100);
		chart.setChartStyle("BucketChart.BucketSeries", "height", 120);
		chart.setDataProvider(createCapacityChart());

		Timer timer = new Timer() {

			@Override
			public void run() {
				chart.setDataProvider(createCapacityChart());
			}
		};

		timer.scheduleRepeating(4000);

		panel.add(chart);

		return panel;
	}

	private Panel createBarChart() {
		VerticalPanel panel = new VerticalPanel();

		final ColumnChartWidget<Map<String, Object>> barChart = new ColumnChartWidget<Map<String, Object>>();
		barChart.setWidth("500");
		barChart.setHeight("300");
		// barChart.enableEffect(true);

		barChart.setDataProvider(createdataList());
		Timer timer = new Timer() {

			@Override
			public void run() {
				barChart.setDataProvider(createdataList());
			}
		};

		timer.scheduleRepeating(4000);

		barChart.setXSeriesProperty("time");
		final CheckBox checkbox = new CheckBox("Enable Animation");
		checkbox.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				barChart.enableEffect(checkbox.getValue());
			}
		});

		panel.add(barChart);
		panel.add(checkbox);
		return panel;
	}

	private Panel createAreaChart() {
		VerticalPanel panel = new VerticalPanel();
		final AreaChartWidget<Map<String, Object>> AreaChart = new AreaChartWidget<Map<String, Object>>();
		AreaChart.setWidth("500");
		AreaChart.setHeight("300");
		// lineChart.enableEffect(true);
		AreaChart.setDataProvider(createdataList());
		AreaChart.setXSeriesProperty("time");

		AreaChart.setChartStyle("AreaSeries", "lineStyle", "curve");

		Timer timer = new Timer() {

			@Override
			public void run() {
				AreaChart.setDataProvider(createdataList());
			}
		};

		timer.scheduleRepeating(4000);

		final CheckBox checkbox = new CheckBox("Enable Animation");
		checkbox.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				AreaChart.enableEffect(checkbox.getValue());
			}
		});
		panel.add(AreaChart);
		panel.add(checkbox);
		return panel;

	}

	private List<List<String>> createCapacityChart() {
		List<List<String>> capacityDP = new ArrayList<List<String>>();

		capacityDP.add(java.util.Arrays.asList("cpu1 utility", ""
				+ getRandomData()));
		capacityDP.add(java.util.Arrays.asList("cpu2 utility", ""
				+ getRandomData()));
		capacityDP.add(java.util.Arrays.asList("cpu3 utility", ""
				+ getRandomData()));
		return capacityDP;
	}

	private List<Map<String, Object>> createdataList() {
		List<Map<String, Object>> list = ChartModelUtil
				.createChartDataProvider("{[time: 1998, OrderNumber: "
						+ getRandomData() + ", CustomerNum: " + getRandomData()
						+ "] " + "[time: 2001, OrderNumber: " + getRandomData()
						+ ", CustomerNum: " + getRandomData() + "]"
						+ " [time: 2003, OrderNumber: " + getRandomData()
						+ ", CustomerNum: " + getRandomData() + "]}");
		return list;
	}

	private int getRandomData() {
		return Math.abs(Random.nextInt()) % 100;
	}

}
